#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
#define PI acos(-1.0)
#define lowerlimit 0
#define upperlimit PI*2
#define secnum 250
double func(double x)
{
	return cos(x);
}
int main(int argc, char** argv)
{
	int size, rank, i;
	double eachlen, eachret, ret;
	double starttime , endtime;
	MPI_Comm comm = MPI_COMM_WORLD;
	MPI_Init(&argc, &argv);
	MPI_Comm_size(comm, &size);
	MPI_Comm_rank(comm, &rank);
	starttime = MPI_Wtime();
	eachlen = (upperlimit-lowerlimit)/(double)size;
	eachret = 0;
	for(i=1; i<=secnum; i++)
		eachret += (eachlen / secnum) * func(lowerlimit + eachlen*rank + i*eachlen/secnum);
	printf("eachret of rank %d is:%lf\n", rank, eachret);
	MPI_Reduce(&eachret, &ret, 1, MPI_DOUBLE, MPI_SUM, 0, comm);
	endtime = MPI_Wtime();
	if(rank == 0)
		printf("ret is:%lf\n",ret);
	printf(" %d %lf\n",rank, endtime - starttime);
	MPI_Finalize();
	return 0;
}